package com.javaforever.dictionary.facade;

import com.javaforever.dictionary.domain.Dictionary;
import com.javaforever.dictionary.service.DictionaryService;
import com.javaforever.dictionary.serviceimpl.DictionaryServiceImpl;
import com.javaforever.dictionary.utils.BooleanUtil;
import com.javaforever.dictionary.utils.StringUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;

public class SearchDictionarysByFieldsByPageFacade extends HttpServlet{

	protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		Map<String,Object> result = new TreeMap<String,Object>();
		try {
			Dictionary dictionary = new Dictionary();
			Long parentId = StringUtil.isBlank(request.getParameter("parentId"))?null:Long.parseLong(request.getParameter("parentId"));
			Long dicGroupId = StringUtil.isBlank(request.getParameter("dicGroupId"))?null:Long.parseLong(request.getParameter("dicGroupId"));
			String dictionaryKey = StringUtil.isBlank(request.getParameter("dictionaryKey"))?null:request.getParameter("dictionaryKey");
			String dictionaryValue = StringUtil.isBlank(request.getParameter("dictionaryValue"))?null:request.getParameter("dictionaryValue");
			String description = StringUtil.isBlank(request.getParameter("description"))?null:request.getParameter("description");
			String dictionaryName = StringUtil.isBlank(request.getParameter("dictionaryName"))?null:request.getParameter("dictionaryName");
			Boolean active = StringUtil.isBlank(request.getParameter("active"))?null:BooleanUtil.parseBoolean(request.getParameter("active"));
			Long pagenum = StringUtil.isBlank(request.getParameter("page"))?1: Long.parseLong(request.getParameter("page"));
			Long pagesize = StringUtil.isBlank(request.getParameter("rows"))?10: Long.parseLong(request.getParameter("rows"));
			Boolean lastFlagBool = StringUtil.isBlank(request.getParameter("last"))?false: BooleanUtil.parseBoolean(request.getParameter("last"));
			
			dictionary.setParentId(parentId);
			dictionary.setDicGroupId(dicGroupId);
			dictionary.setDictionaryKey(dictionaryKey);
			dictionary.setDictionaryValue(dictionaryValue);
			dictionary.setDescription(description);
			dictionary.setDictionaryName(dictionaryName);
			dictionary.setActive(active);
			DictionaryService service = new DictionaryServiceImpl();
			Long recordCount = service.countSearchDictionarysByFieldsRecords(dictionary);
			Long pageCount = (long)Math.ceil((double)recordCount/pagesize);
			if (pageCount <= 1) pageCount = 1L;
			if (pagenum==null || pagenum <= 1) pagenum = 1L;
			if (pagenum >= pageCount) pagenum = pageCount;
			if (lastFlagBool) pagenum = pageCount;
			List<Dictionary> list = service.searchDictionarysByFieldsByPage(dictionary,pagenum,pagesize);
			result.put("success",true);
			result.put("rows",list);
			result.put("pagesize",pagesize);
			result.put("page",pagenum);
			result.put("total",recordCount);
			out.print(JSONObject.fromObject(result).toString());
			} catch (Exception e){
				e.printStackTrace();
				result.put("success",false);
				result.put("reason",e.getMessage());
				out.print(JSONObject.fromObject(result).toString());
			} finally {
				out.close();
			}
	}

	public String getServletInfo(){
		return "Powered by Mind Rules.";
	}

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		processRequest(request, response);
	}

}
